package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if (employee != null){
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息");
    }

    @PutMapping
    public R update(HttpServletRequest request,@RequestBody Employee employee){
//        employee.setUpdateTime(LocalDateTime.now());
//        Long employeeID = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateUser(employeeID);
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    @GetMapping("/page")
    public R page(int page,int pageSize,String name){
        log.info("分页查询员工page {}, pageSize {},name {}",page, pageSize, name);
        Page employeePage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        lqw.orderByDesc(Employee::getUpdateTime);
        employeeService.page(employeePage,lqw);
        return R.success(employeePage);
    }

    @PostMapping
    public R save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("添加员工请求,新员工为employee {}",employee);
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        Long employeeID = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(employeeID);
//        employee.setUpdateUser(employeeID);
        employeeService.save(employee);
        return R.success("添加成功");
    }

    //登录
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,employee.getUsername());
        Employee employeeDb = employeeService.getOne(lqw);
        if (employeeDb == null){
            return R.error("用户名错误");
        }
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employeeDb.getPassword())){
            return R.error("密码错误");
        }
        if (employeeDb.getStatus() == 0){
            return R.error("登陆失败");
        }
        request.getSession().setAttribute("employee",employeeDb.getId());
        return R.success(employeeDb);
    }

    //退出登录
    @PostMapping("/logout")
    public R logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("成功退出");
    }
}
